package com.zhkj.txg.module.member.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.base.QueryShippingResponse
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.member.entity.*
import com.zhkj.txg.module.member.model.MemberManager

class MemberListViewModel(application: Application) : BaseViewModel(application) {
    private val manager = MemberManager()
    val inviteListLiveData = MutableLiveData<HttpResponse<InviteResponse>>()
    val rebateListLiveData = MutableLiveData<HttpResponse<RebateLogResponse>>()
    val rebateStatisticsLiveData = MutableLiveData<HttpResponse<RebateStatisticsResponse>>()
    val orderListLiveData = MutableLiveData<HttpResponse<MemberOrderResponse>>()
    val memberOrderDetailLiveData = MutableLiveData<HttpResponse<MemberOrderDetailResponse>>()
    val memberReceiveLiveData = MutableLiveData<HttpResponse<BaseResponse>>()
    val shippingLiveData = MutableLiveData<HttpResponse<QueryShippingResponse>>()

    private val numb = 20
    var page = 1
        private set

    fun inviteList(type: Int, restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<InviteResponse>(inviteListLiveData) {
            override fun onSuccess(resp: InviteResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val inviteList = manager.inviteList(AccountManager.getToken(), page, numb, type, listener)
        dispose.add(inviteList)
    }

    fun rebateList(type: Int, time: String, restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<RebateLogResponse>(rebateListLiveData) {
            override fun onSuccess(resp: RebateLogResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val rebateLog = manager.rebateLog(AccountManager.getToken(), page, numb, type, time, listener)
        dispose.add(rebateLog)
    }

    fun rebateStatistics() {
        val listener = object : ViewModelSubscribeListener<RebateStatisticsResponse>(rebateStatisticsLiveData) {
            override fun onSuccess(resp: RebateStatisticsResponse) {
                val application = getApplication<Application>()
                resp.data?.today?.typeString = application.getString(R.string.income_today)
                resp.data?.yesterday?.typeString = application.getString(R.string.income_yesterday)
                resp.data?.month?.typeString = application.getString(R.string.income_month)
                resp.data?.lastMonth?.typeString = application.getString(R.string.income_last_month)
                super.onSuccess(resp)
            }
        }
        val rebateStatistics = manager.rebateStatistics(AccountManager.getToken(), listener)
        dispose.add(rebateStatistics)
    }

    fun orderList(restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<MemberOrderResponse>(orderListLiveData) {
            override fun onSuccess(resp: MemberOrderResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val rebateLog = manager.memberOrderList(AccountManager.getToken(), page, numb, listener)
        dispose.add(rebateLog)
    }

    fun memberOrderDetail(orderId: Long) {
        val listener = object : ViewModelSubscribeListener<MemberOrderDetailResponse>(memberOrderDetailLiveData) {
            override fun onSuccess(resp: MemberOrderDetailResponse) {
                super.onSuccess(resp)
                if (!resp.data?.shippingNo.isNullOrEmpty() && !resp.data?.shippingCode.isNullOrEmpty())
                    queryShipping(resp.data?.shippingNo ?: "", resp.data?.shippingCode ?: "")
            }
        }
        val memberOrderDetail = manager.memberOrderDetail(AccountManager.getToken(), orderId, listener)
        dispose.add(memberOrderDetail)
    }

    fun memberReceive(orderId: Long) {
        val listener = ViewModelSubscribeListener(memberReceiveLiveData)
        val memberOrderDetail = manager.memberReceive(AccountManager.getToken(), orderId, listener)
        dispose.add(memberOrderDetail)
    }

    fun queryShipping(shippingNo: String, shippingCode: String) {
        val queryShipping = manager.queryShipping(
            AccountManager.getToken(),
            shippingNo,
            shippingCode,
            ViewModelSubscribeListener(shippingLiveData)
        )
        dispose.add(queryShipping)
    }
}